package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.Result;
import com.itheima.pojo.Advice;
import com.itheima.service.AdviceService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/advice")
public class AdviceController {

    @Reference
    private AdviceService adviceService;

    @RequestMapping("/add")
    public Result add(@RequestBody Advice advice){
        try {
            adviceService.add(advice);
            return new Result(true,"健康提示添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"健康提示添加失败！");
        }
    }


    @RequestMapping("/findByOrderId")
    public Result findByOrderId(int orderId){
        int count = adviceService.findByOrderId(orderId);
        if (count == 0){
            return new Result(true,"可以添加");
        }else {
            return new Result(false,"该用户已经进行了健康干预，请勿重复操作");
        }
    }

    @RequestMapping("/findAllReportByOrderId")
    public Result findAllReportByOrderId(int orderId){
        try {
            Advice advice = adviceService.findAllReportByOrderId(orderId);
            return new Result(true,"获取数据成功",advice);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"获取数据失败");
        }
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Advice advice){
        try {
            adviceService.edit(advice);
            return new Result(true,"健康提示修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"健康提示修改失败！");
        }
    }
}
